package com.yzycoc.coc.gf.function;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONObject;
import com.yzycoc.coc.gf.config.CocConfig;
import com.yzycoc.coc.gf.constant.ApiUrlConstant;
import com.yzycoc.coc.gf.info.Result;
import com.yzycoc.coc.gf.info.ClanImageInfo;
import com.yzycoc.coc.gf.info.LexiconInfo;
import com.yzycoc.coc.gf.service.CocService;
import com.yzycoc.coc.gf.util.CocUtil;
import com.yzycoc.code.special.ApiSpecialCode;
import com.yzycoc.processor.event.EventPrivateMessage;
import com.yzycoc.processor.send.info.ClientInfos;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 部落相关功能
 * 创建时间： 2023/9/7
 *
 * @author yzy
 * @version 1.0
 **/
@Slf4j
public class CocPrivateFunction {


    public static boolean clans(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        String tag = split[2];
        if (!CocUtil.isTag(tag)) {
            ClientInfos clientInfos = CocConfig.API.clientInfos(EventPrivateMessage.getClient());
            if (ObjectUtil.isEmpty(clientInfos)) {
                CocConfig.API.sendPrivateMsg("当前存在非法未注册的组件，无法进行接下来的操作！", EventPrivateMessage);
                return true;
            }
            //查询绑定
            Result<JSONObject> cocBindInfo = BinDingPublicFunction.get(EventPrivateMessage.getSendUser(), tag, "部落", clientInfos.getClientType());
            if (!cocBindInfo.isSuccess()) {
                CocConfig.API.sendPrivateMsg(cocBindInfo.getMessage(), EventPrivateMessage);
                return true;
            }
            tag = cocBindInfo.getResult().getString("tag");
        }
        tag = tag.replace("#", "");
        CocConfig.API.sendPrivateMsg("正在生成中部落(#" + tag + ")信息,请耐心等待！", EventPrivateMessage);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("tag", tag);
        ClanImageInfo clan = CocService.httpApi(ApiUrlConstant.IMAGE_CLAN, paramMap);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
        }
        return true;
    }


    public static boolean player(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        String tag = split[2];
        if (!CocUtil.isTag(tag)) {
            ClientInfos clientInfos = CocConfig.API.clientInfos(EventPrivateMessage.getClient());
            if (ObjectUtil.isEmpty(clientInfos)) {
                CocConfig.API.sendPrivateMsg("当前存在非法未注册的组件，无法进行接下来的操作！", EventPrivateMessage);
                return true;
            }
            //查询绑定
            Result<JSONObject> cocBindInfo = BinDingPublicFunction.get(EventPrivateMessage.getSendUser(), tag, "玩家", clientInfos.getClientType());
            if (!cocBindInfo.isSuccess()) {
                CocConfig.API.sendPrivateMsg(cocBindInfo.getMessage(), EventPrivateMessage);
                return true;
            }
            tag = cocBindInfo.getResult().getString("tag");
        }
        tag = tag.replace("#", "");
        CocConfig.API.sendPrivateMsg("正在生成中玩家(#" + tag + ")信息,请耐心等待！", EventPrivateMessage);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("tag", tag);
        ClanImageInfo clan = CocService.httpApi(ApiUrlConstant.IMAGE_PLAYER, paramMap);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
        }
        return true;
    }

    public static boolean clansName(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        Map<String, Object> paramMap = new HashMap<>();
        StringBuffer name = new StringBuffer();
        for (int i = 1; i < split.length; i++) {
            if (i != 1) {
                name.append("#");
            }
            name.append(split[i]);
        }
        paramMap.put("name", name.toString());
        String body = HttpRequest.get(CocConfig.API_URL + ApiUrlConstant.NAME_CLAN)
                .form(paramMap)
                .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).timeout(30000).execute().body();
        ClanImageInfo clan = JSONObject.parseObject(body, ClanImageInfo.class);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
        }
        return true;
    }

    public static boolean playerName(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        CocConfig.API.sendPrivateMsg("正在查询玩家名,请耐心等待！", EventPrivateMessage);
        Map<String, Object> paramMap = new HashMap<>();
        StringBuffer name = new StringBuffer();
        for (int i = 1; i < split.length; i++) {
            if (i != 1) {
                name.append("#");
            }
            name.append(split[i]);
        }
        paramMap.put("name", name.toString());
        String body = HttpRequest.get(CocConfig.API_URL + ApiUrlConstant.NAME_PLAYER)
                .form(paramMap)
                .header(ApiUrlConstant.TOKEN_AUTHENTICATION, CocConfig.AUTH).timeout(30000).execute().body();
        ClanImageInfo clan = JSONObject.parseObject(body, ClanImageInfo.class);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
            return true;
        }
    }

    public static boolean clanAllCollect(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        String tag = split[2];
        if (!CocUtil.isTag(tag)) {
            ClientInfos clientInfos = CocConfig.API.clientInfos(EventPrivateMessage.getClient());
            if (ObjectUtil.isEmpty(clientInfos)) {
                CocConfig.API.sendPrivateMsg("当前存在非法未注册的组件，无法进行接下来的操作！", EventPrivateMessage);
                return true;
            }
            //查询绑定
            Result<JSONObject> cocBindInfo = BinDingPublicFunction.get(EventPrivateMessage.getSendUser(), tag, "部落配置", clientInfos.getClientType());
            if (!cocBindInfo.isSuccess()) {
                CocConfig.API.sendPrivateMsg(cocBindInfo.getMessage(), EventPrivateMessage);
                return true;
            }
            tag = cocBindInfo.getResult().getString("tag");
        }
        tag = tag.replace("#", "");
        CocConfig.API.sendPrivateMsg("正在查询部落(#" + tag + "),需要等待5-30s....", EventPrivateMessage);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("tag", tag);
        ClanImageInfo clan = CocService.httpApi(ApiUrlConstant.IMAGE_CLAN_ALL_COLLECT_IMAGE, paramMap);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
        }
        return true;
    }

    public static boolean clanAll(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        String tag = split[2];
        if (!CocUtil.isTag(tag)) {
            ClientInfos clientInfos = CocConfig.API.clientInfos(EventPrivateMessage.getClient());
            if (ObjectUtil.isEmpty(clientInfos)) {
                CocConfig.API.sendPrivateMsg("当前存在非法未注册的组件，无法进行接下来的操作！", EventPrivateMessage);
                return true;
            }
            //查询绑定
            Result<JSONObject> cocBindInfo = BinDingPublicFunction.get(EventPrivateMessage.getSendUser(), tag, "部落", clientInfos.getClientType());
            if (!cocBindInfo.isSuccess()) {
                CocConfig.API.sendPrivateMsg(cocBindInfo.getMessage(), EventPrivateMessage);
                return true;
            }
            tag = cocBindInfo.getResult().getString("tag");
        }
        tag = tag.replace("#", "");
        CocConfig.API.sendPrivateMsg("正在查询部落(#" + tag + ")科技,需要等待5-30s....", EventPrivateMessage);
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("tag", tag);
        ClanImageInfo clan = CocService.httpApi(ApiUrlConstant.IMAGE_CLAN_ALL, paramMap);
        if (clan.getSuccess()) {
            String result = ApiSpecialCode.IMAGE(clan.getHttp());
            CocConfig.API.sendPrivateMsg(result, EventPrivateMessage);
            return true;
        } else {
            CocConfig.API.sendPrivateMsg(clan.getResult(), EventPrivateMessage);
        }
        return true;
    }

    public static boolean lexicon(EventPrivateMessage EventPrivateMessage, String msg, String[] split) {
        String text = split[1];
        List<LexiconInfo> lexiconInfos = CocConfig.lexiconInfo.get(text);
        log.debug("参与处理数据：{},接收到数据{}", text, lexiconInfos);
        if (CollUtil.isNotEmpty(lexiconInfos)) {
            Boolean isSend = false;
            for (LexiconInfo lexiconInfo : lexiconInfos) {
                String response = lexiconInfo.getResponse();
                String[] resSplit = response.split("#分割#");
                if (resSplit.length > 1) {
                    Random random = new Random();
                    int randomIndex = random.nextInt(resSplit.length);
                    response = resSplit[randomIndex];
                }
                if ("全部".equals(lexiconInfo.getType())) {
                    CocConfig.API.sendPrivateMsg(response, EventPrivateMessage);
                    isSend = true;
                } else {
                    ClientInfos clientInfos = CocConfig.API.clientInfos(EventPrivateMessage.getClient());
                    if (clientInfos != null && clientInfos.getClientType().equals(lexiconInfo.getType())) {
                        CocConfig.API.sendPrivateMsg(response, EventPrivateMessage);
                        isSend = true;
                    }
                }
            }
            return isSend;
        }
        return false;
    }

}
